﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlLib
{
    /// <summary>
    /// NumberFoldingCard.xaml 的交互逻辑
    /// </summary>
    public partial class NumberFoldingCard : UserControl
    {
        public NumberFoldingCard()
        {
            InitializeComponent();
        }

        public string Number
        {
            get { return (string)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }

        public static readonly DependencyProperty NumberProperty =
            DependencyProperty.Register("Number", typeof(string), typeof(NumberFoldingCard), new PropertyMetadata("10"));

        public string NextNumber
        {
            get { return (string)GetValue(NextProperty); }
            set { SetValue(NextProperty, value); }
        }

        public static readonly DependencyProperty NextProperty =
            DependencyProperty.Register("NextNumber", typeof(string), typeof(NumberFoldingCard), new PropertyMetadata("0"));


        public static readonly DependencyProperty IsFlippingProperty =
            DependencyProperty.Register("IsFlipping", typeof(bool), typeof(NumberFoldingCard), new PropertyMetadata(false));

        public bool IsFlipping
        {
            get { return (bool)GetValue(IsFlippingProperty); }
            set { SetValue(IsFlippingProperty, value); }
        }

        public static readonly DependencyProperty FlipDurationProperty =
            DependencyProperty.Register("FlipDuration", typeof(Duration), typeof(NumberFoldingCard), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0.3))));

        public Duration FlipDuration
        {
            get { return (Duration)GetValue(FlipDurationProperty); }
            set { SetValue(FlipDurationProperty, value); }
        }

        public void StartFlip()
        {
            if (IsFlipping)
                return;

            IsFlipping = true;

            var animation = new DoubleAnimation
            {
                From = 0,
                To = 180,
                Duration = FlipDuration,
                FillBehavior = FillBehavior.Stop
            };

            animation.Completed += (sender, e) =>
            {
                Number = NextNumber;
                PART_AxisAngleRotation3D.Angle = 0;

                IsFlipping = false;
            };

            PART_AxisAngleRotation3D.BeginAnimation(AxisAngleRotation3D.AngleProperty, animation);
        }
    }
}
